using UnityEngine;
using UnityEngine.UI;
using QFramework;
using Unity.VisualScripting;

namespace Survivor
{
    public class UIGamePanelData : UIPanelData
    {
    }

    public partial class UIGamePanel : UIPanel
    {
        protected override void OnInit(IUIData uiData = null)
        {
            mData = uiData as UIGamePanelData ?? new UIGamePanelData();

            Global.Hp.RegisterWithInitValue(hp =>
            {
                HPTxt.text = $"生命值: {hp}/{Global.MaxHp.Value}";
            }).UnRegisterWhenGameObjectDestroyed(this);

            Global.MaxHp.RegisterWithInitValue(maxHp =>
            {
                HPTxt.text = $"生命值: {Global.Hp}/{maxHp}";
            }).UnRegisterWhenGameObjectDestroyed(this);
            
            Global.Exp.RegisterWithInitValue(exp => { ExpTxt.text = $"经验值: {exp}/{Global.ExpToNextLevel()}"; })
                .UnRegisterWhenGameObjectDestroyed(this);

            Global.Level.RegisterWithInitValue(level => { LevelTxt.text = $"等级: {level}"; })
                .UnRegisterWhenGameObjectDestroyed(this);

            EnemyGenerator.EnemyCnt.RegisterWithInitValue(enemyCnt =>
            {
                EnemyCntTxt.text = $"敌人: {enemyCnt}";
            }).UnRegisterWhenGameObjectDestroyed(this);

            Global.Level.Register((level) =>
            {
                Time.timeScale = 0;
                ExpUpgradePanel.Show();
                AudioKit.PlaySound("LevelUp");
            }).UnRegisterWhenGameObjectDestroyed(this);

            Global.Exp.RegisterWithInitValue(exp =>
            {
                if (exp >= Global.ExpToNextLevel())
                {
                    Global.Exp.Value -= Global.ExpToNextLevel();
                    Global.Level.Value++;
                }
            }).UnRegisterWhenGameObjectDestroyed(this);

            // SimpleDurationAbilityBtn.onClick.AddListener(()=>
            // {
            //     Time.timeScale = 1;
            //     Global.SimpleAbilityDuration.Value *= 0.8f;
            //     AbilityRoot.Hide();
            //     AudioKit.PlaySound("AbilityLevelUp");
            // });

            Global.CurrentSeconds.RegisterWithInitValue(currentSeconds =>
            {
                if (Time.frameCount % 30 == 0)
                {
                    int minutes = (int) currentSeconds / 60;
                    int seconds = (int) currentSeconds % 60;
                    TimeTxt.text = $"时间: {minutes:00}:{seconds:00}";
                }
            }).UnRegisterWhenGameObjectDestroyed(this);

            var enemyGenerator = FindObjectOfType<EnemyGenerator>();

            ActionKit.OnUpdate.Register(() =>
            {
                Global.CurrentSeconds.Value += Time.deltaTime;

                if (enemyGenerator.IsLastWave && EnemyGenerator.EnemyCnt.Value <= 0 && EnemyGenerator.CurrentEnemyWave == null)
                {
                    if (UIKit.GetPanel<UIGamePanel>().IsNull())
                    {
                        UIKit.OpenPanel<UIGamePassPanel>();
                    }
                }
            }).UnRegisterWhenGameObjectDestroyed(this);

            Global.Coin.RegisterWithInitValue(coin =>
            {
                CoinTxt.text = $"金币: {coin}";
            }).UnRegisterWhenGameObjectDestroyed(this);
        }

        protected override void OnOpen(IUIData uiData = null)
        {
        }

        protected override void OnShow()
        {
        }

        protected override void OnHide()
        {
        }

        protected override void OnClose()
        {
        }
    }
}